package com.imay.live.logic;

import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;

import com.imay.dialog.ActionSheetDialog;
import com.imay.dialog.listener.OnOperItemClickL;
import com.imay.live.activity.ProfileEditActivity;
import com.imay.live.manager.UploadFileManager;
import com.imay.live.utils.ToastUtil;
import com.imay.live.view.wheel.view.MainDialog;
import com.imay.live.view.wheel.widget.curved.WheelYeasPickerContainer;
import com.imay.live.R;

import java.util.Calendar;

/**
 * Created by  nick on 2017/3/31 0031.
 * 封装activity 操作逻辑的类
 */

public class ProfileEditActivityLogic implements MainDialog.ClickCallBack {

    private ProfileEditActivity activity;
    private MainDialog dialog;

    public ProfileEditActivityLogic(ProfileEditActivity activity) {
        this.activity = activity;
    }


    // 上传头像
    public void upload() {
        UploadFileManager.getInstance().uploadHead(activity.getImagePath(), new UploadFileManager.UploadCallBack() {
            @Override
            public void uploadSuccess(String fileUrl) {
                activity.setResultUrl(fileUrl);
                ToastUtil.show(activity, "上传成功");
            }

            @Override
            public void uploadFailer(int errorCode, String errorMessage) {
                ToastUtil.show(activity, "上传失败");
            }
        });
    }

    public void showWheelView(int age) {
        if (dialog == null) {
            dialog = new MainDialog(activity, this);
        }
        int padding = activity.getResources().getDimensionPixelSize(R.dimen.WheelPadding);
        int textSize = activity.getResources().getDimensionPixelSize(R.dimen.TextSizeLarge);
        int itemSpace = activity.getResources().getDimensionPixelSize(R.dimen.ItemSpaceLarge);
        WheelYeasPickerContainer wheelDatePicker = new WheelYeasPickerContainer(activity);
        android.view.WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        wheelDatePicker.setLayoutParams(lp);

        wheelDatePicker.setPadding(padding, 0, padding, 0);
        wheelDatePicker.setBackgroundColor(activity.getResources().getColor(R.color.divider_ed));
        wheelDatePicker.setTextColor(0xC0C0C0);
        wheelDatePicker.setCurrentTextColor(0xFF000000);

        wheelDatePicker.setTextSize(textSize);
        wheelDatePicker.setItemSpace(itemSpace);
        Calendar calendar = Calendar.getInstance();
        if (age>0){
            calendar.add(Calendar.YEAR, -age);
            wheelDatePicker.setCurrentDate(calendar.get(Calendar.YEAR));
        }else{
            wheelDatePicker.setCurrentDate(1990);
        }
        dialog.setContentView(wheelDatePicker);

        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow()
                .getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(params);
    }


    public String getSex(int sex) {
        String sexStr;
        switch (sex) {
            case 1:
                sexStr = activity.getString(R.string.profile_sex_man);
                break;
            case 2:
                sexStr = activity.getString(R.string.profile_sex_woman);
                break;
            default:
                sexStr = activity.getString(R.string.profile_sex_unknow);

        }
        return sexStr;

    }

    public void showChooseSexDailog() {
        final String[] stringItems = {activity.getString(R.string.profile_sex_man), activity.getString(R.string.profile_sex_woman)};
        final ActionSheetDialog dialog = new ActionSheetDialog(activity, stringItems, null);
        dialog.isTitleShow(false).show();
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                activity.setSex(position + 1);
                dialog.dismiss();
            }
        });
    }


    @Override
    public void onClick(String str) {
        int year = Integer.parseInt(str);
        int age = Calendar.getInstance().get(Calendar.YEAR) - year;
        activity.setAge(age);
    }


}
